﻿using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class PositionController : Controller
    {
        private PositionServers positionServers;

        public PositionController()
        {
            positionServers = new PositionServers();
        }

        // GET: System/Position
        public ActionResult Index()
        {
            ViewBag.DEPT_IDData = JsonConvert.SerializeObject(DeptTreeData());
            return View();
        }

        // GET: System/Position/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: System/Position/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: System/Position/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: System/Position/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.TreeData = DeptTreeData();
            var model = new position();
            if (id != null)
            {
                ViewBag.Title = "编辑职务";
                model = GetPositionModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建职务";
                model = GetPositionModel();
            }

            return View(model);
        }

        // POST: System/Position/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, position model)
        {
            ViewBag.TreeData = DeptTreeData();
            try
            {
                if (id == null || id == 0)
                {
                    positionServers.Add(model);
                    ViewBag.msg = "新增职务信息成功！";
                    model = new position();

                }
                else
                {
                    positionServers.Update(model);
                    ViewBag.msg = "修改职务信息成功！";

                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }


        // GET: System/Position/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                positionServers.Del(id.ToString());
                ViewBag.msg = "删除职务信息成功！";
            }
            return Content("");
        }
    

        // POST: System/Position/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult SelectDataRight(string id)
        {
            ViewBag.right = id??"";
            return View();
        }


        #region //GetDate

        public string GetTableJsonResult()
        {
            var list = positionServers.GetPositions();

            return JsonConvert.SerializeObject(list);
        }

        public position GetPositionModel(string id = null)
        {
            var model = new position();
            if (id != null)
                model = positionServers.GetPositionById(id);
            return model;
        }

        #region 绑定部门=============================

        private List<SelectListItem> DeptTreeData()
        {
            List<SelectListItem> menus = new List<SelectListItem>();
            var list = positionServers.GetDepts();

            menus.Clear();
            menus.Add(new SelectListItem() { Value = "0", Text = "无父级部门" });

            foreach (var dr in list)
            {
                string Id = dr.DEPT_ID.ToString();
                int ClassLayer = 1;// int.Parse(dr.LEVEL.ToString());
                string Title = dr.DEPT_NAME.ToString().Trim();

                if (ClassLayer == 0)
                {
                    menus.Add(new SelectListItem { Value = Id, Text = Title });
                }
                else
                {
                    Title = "├ " + Title;
                    Title = StringUtil.StringOfChar(ClassLayer, "　") + Title;
                    menus.Add(new SelectListItem { Value = Id, Text = Title });
                }
            }

            return menus;
        }


        public ActionResult GetjsonTreeData(string idstr = null)
        {
            var list = GetDepts(0, idstr);
            return Json(list, JsonRequestBehavior.AllowGet);
        }


        public List<TreeViewNode> GetDepts(int parentId,string idstr)
        {
            List<TreeViewNode> tlist = new List<TreeViewNode>();
            var list = positionServers.GetDepts();           
            var deptlist = list.FindAll(m => m.DEPT_PARENT == parentId);
            if(deptlist!=null&& deptlist.Count>0)
            {
                foreach (var item in deptlist)
                {
                    TreeViewNode treeViewNode = new TreeViewNode
                    {
                        text = item.DEPT_NAME,
                        id = "D" + item.DEPT_ID.ToString(),
                        icon = "glyphicon glyphicon-equalizer",
                        nodes = new List<TreeViewNode>()
                    };
                    var personlist = GetPerson(item.DEPT_ID, idstr);
                    if (personlist != null && personlist.Count > 0)
                    {
                        treeViewNode.nodes.AddRange(personlist);
                    }
                    var subdeptlist = GetDepts(item.DEPT_ID, idstr);
                    if(subdeptlist!=null && subdeptlist.Count>0)
                    {
                        treeViewNode.nodes.AddRange(subdeptlist);
                    }
                    if (!string.IsNullOrEmpty(idstr))
                    {
                        if (idstr.Contains("D" + item.DEPT_ID.ToString()))
                        {
                            treeViewNode.state = new State { @checked= true };
                        }
                           
                    }

                    tlist.Add(treeViewNode);
                 }
            }
            return tlist;

        }

        public List<TreeViewNode> GetPerson(int parentId, string idstr)
        {
            List<TreeViewNode> tlist = new List<TreeViewNode>();
            var list = positionServers.GetUsers().FindAll(m=>m.DEPT_ID== parentId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    TreeViewNode treeViewNode = new TreeViewNode
                    {
                        text = item.TRUE_NAME,
                        id = "U" + item.USER_ID.ToString(),
                        icon = "glyphicon glyphicon-user"
                    };
                    if (!string.IsNullOrEmpty(idstr))
                    {
                        if (idstr.Contains("U" + item.DEPT_ID.ToString()))
                        {
                            treeViewNode.state = new State { @checked= true };
                        }

                    }
                    tlist.Add(treeViewNode);
                }
            }
            return tlist;
        }

        #endregion 绑定部门=============================

        #endregion //GetDate
    }
}